import { useSafeState, useMemoizedFn, useMount, useUnmount } from 'ahooks';
import { loadingEvent, eventType } from '@/components/Loading';

export const useLoadingState = () => {
  const [loading, setLoading] = useSafeState(false);

  const onLoading = useMemoizedFn((state: boolean) => {
    setLoading(state);
  });

  useMount(() => {
    loadingEvent.addListener(eventType, onLoading);
  });

  useUnmount(() => {
    loadingEvent.removeListener(eventType, onLoading);
  });

  return [loading];
}